package com.lk.oauth2.server.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @description: 安全配置类
 * @author: Li Kang
 * @create: 2020-08-30 10:22
 */
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
  @Autowired private PasswordEncoder passwordEncoder;

  @Autowired private UserDetailsService customUserDetailsService;

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    //  内存方式：用户名和密码
    /*    auth.inMemoryAuthentication()
    .withUser("admin")
    .password(passwordEncoder.encode("123456"))
    .authorities("product");*/

    auth.userDetailsService(customUserDetailsService);
  }

  /** password 密码模式要使用此认证管理器 */
  @Bean
  @Override
  public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
  }
}
